#!/bin/sh
# 说明：Kylin10操作系统 环境初始化
# by: jiwei

#TODO 主机名不能有下划线
# TODO 时钟同步
name="datacube_test"
pass="y93exp3IsHBFUrFA"
hosts=(
  "173.18.162.30 datacube-30.test.sdses.local"
  "173.18.162.31 datacube-31.test.sdses.local"
  "173.18.162.32 datacube-32.test.sdses.local"
  "173.18.162.33 datacube-33.test.sdses.local"
  "173.18.162.34 datacube-34.test.sdses.local"
  "173.18.162.35 datacube-35.test.sdses.local"
  "173.18.162.36 datacube-36.test.sdses.local"
  "173.18.162.37 datacube-37.test.sdses.local"
)

# 如果安装doris配置这个参数：
#sudo sysctl -w vm.max_map_count=2000000
#sudo vim /etc/sysctl.conf
#vm.max_map_count=2000000
#sysctl -p

echo "步骤1/11 : 创建管理用户,请稍等........."
echo "you are setting username : $name"
echo "you are setting password : $pass for $name"

sudo useradd $name -d /data/$name
if [ $? -eq 0 ];then
   echo "user ${name} is created successfully!!!"
else
   echo "user ${name} is created failly!!!"
   exit 1
fi
echo $pass | sudo passwd $name --stdin &>/dev/null
if [ $? -eq 0 ];then
   echo "${name}'s password is set successfully"
else
   echo "${name}'s password is set failly!!!"
fi

echo "步骤2/11 : 下载常用工具，请稍后"
yum -y install gcc gcc-c++ net-tools make autoconf sysstat bash-completion wget vim telnet libcurl lrzsz dos2unix nohup unzip openssl-devel lsof lvm2&>/dev/null

# 3. TODO 更改源[根据操作系统更改]
echo "步骤3/11 : 更换为Yum、epel源,请稍等....... "
mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak
cat >> /etc/yum.repos.d/kylin.repo << EOF
[kylin]
name=kylin
baseurl=http://172.31.0.16:8077/kylin
enabled=1
gpgcheck=0
EOF
yum clean all
yum makecache

echo "步骤4/11 : 修改字符集........... "
sed -i 's/LANG="en_US.UTF-8"/LANG="zh_CN.UTF-8"/' /etc/locale.conf

echo "步骤5/11 : 同步时间,请稍等....... "
if [ -f /usr/sbin/ntpdate ];then
  echo ""
else
  yum -y install ntp &>/dev/null
fi
/usr/sbin/ntpdate cn.pool.ntp.org &>/dev/null
echo "0 * * * *  /usr/sbin/ntpdate time1.aliyun.com >> /tmp/autontpdate 2>&1" >> /var/spool/cron/root
systemctl restart crond.service
0 * * * *  /usr/sbin/ntpdate 192.168.102.161 >> /tmp/autontpdate 2>&1

echo "步骤6/11 : 设置上海时区......."
timedatectl set-timezone Asia/Shanghai

echo "步骤7/11 : 内核参数优化,请稍等......."
echo "ulimit -SHn 102400" >> /etc/rc.local
echo "vm.max_map_count=65535" >>/etc/sysctl.conf
echo "net.core.somaxconn=65535" >>/etc/sysctl.conf
echo "vm.overcommit_memory=1" >>/etc/sysctl.conf
echo "vm.swappiness=10">> /etc/sysctl.conf
sysctl -p

echo "步骤8/11 : 设置最大打开文件描述符数,请稍等......."
cat >> /etc/security/limits.conf << EOF
* soft nofile 655350
* hard nofile 655350
* soft nproc 655350
* hard nproc 655350
* soft stack unlimited
* hard memlock unlimited
EOF

echo "步骤9/11 : 禁止相关服务"
echo "9.1禁止定时任务向发送邮件,请稍等......"
sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab

# 重启服务器生效
echo "9.2禁用SWAP使用,请稍等........."
sed -i '/swap/s/^/#/' /etc/fstab
swapoff -a

# 重启服务器生效
echo "9.3禁用selinux,请稍等......."
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
systemctl stop firewalld
systemctl disable firewalld

echo "9.4关闭防火墙,请稍等......."
systemctl stop firewalld
systemctl disable firewalld

echo "9.5禁用透明大页,请稍等......."
echo never > /sys/kernel/mm/transparent_hugepage/defrag
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo 'echo never > /sys/kernel/mm/transparent_hugepage/defrag' >> /etc/rc.d/rc.local
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

# TODO 修改hostname格式
echo "步骤10/11 : 修改hostname, hosts......."
sed -i '1,2 s/^/# /' /etc/hosts
ip=$(ip addr show | grep 'inet ' | grep -v '127\.0\.0\.1' | head -n1 | awk '{print $2}' | cut -f1 -d'/')
hostnamectl set-hostname "datacube-${ip##*.}.test.local"

for h in "${hosts[@]}"
do
  echo "$h" | sudo tee -a /etc/hosts > /dev/null
done
cat /etc/hosts

echo "步骤11/11 : 重启系统......";
#reboot